import datetime
from flask_login import UserMixin
from sqlalchemy import DateTime, Numeric
from werkzeug.security import generate_password_hash, check_password_hash
from applications.exts import db


class UserLevel(db.Model):
    __tablename__ = 'userlevel'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='购物车id')
    openid = db.Column(db.String(60), db.ForeignKey('wx_user.openid'), comment='用户openid')
    level = db.Column(db.Integer,default=0, comment='0,普通会员，1白银会员，2黄金会员')
    integral = db.Column(db.Integer,default=0, comment='积分')

    # status=db.Column(db.Integer,comment='积分状态')

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res
